package com.example.module_notice.ui

import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.base.BaseFragment
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.utils.LoggerUtils
import com.example.lib_common.utils.RouterPath
import com.example.module_notice.R
import com.example.module_notice.adapter.NoticeAdapter
import com.example.module_notice.databinding.FragmentNoticeBinding
import com.example.module_notice.ui.notice.NoticeViewModel
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint


val TAG = "notice-log---"


@AndroidEntryPoint
class NoticeFragment : BaseFragment<NoticeViewModel,FragmentNoticeBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_notice

    private val mViewModel : NoticeViewModel by viewModels()
    private lateinit var mAdapter: NoticeAdapter

    override fun initView() {
        val userId = MMKV.defaultMMKV().decodeInt("userid")
        if (userId != 0){
            mViewModel.getNoticeList(userId)
            mBinding.noticeRv.visibility = View.VISIBLE
            mBinding.noticeNone.visibility = View.GONE
            mBinding.noticeNoneText.visibility = View.GONE
            mBinding.gotoLogin.visibility = View.GONE
            initAdapter()
            initNoticeItem()
        }else{
            mBinding.noticeRv.visibility = View.GONE
            mBinding.noticeNone.visibility = View.VISIBLE
            mBinding.noticeNoneText.visibility = View.VISIBLE
            mBinding.gotoLogin.visibility = View.VISIBLE
        }

    }

    override fun initData() {
        mBinding.gotoLogin.setOnClickListener {
            gotoLogin()
        }
    }

    private fun initAdapter(){
        //适配器初始化
        mAdapter = NoticeAdapter()
        mBinding.noticeRv.layoutManager = LinearLayoutManager(requireContext())
        mBinding.noticeRv.adapter = mAdapter
    }

    private fun initNoticeItem(){
        mViewModel.mNoticeState.observeWithLifecycle(this){
            when(it){
                is Empty -> {
                    LoggerUtils.d(TAG,"加载成功1         "+mViewModel.mNoticeList[0].title)
                }
                is Loading -> {
                    LoggerUtils.d(TAG,"加载成功2")
                }
                is Success -> {
                    if (mAdapter.itemCount == 0){
                        mBinding.noticeRv.visibility = View.GONE
                        mBinding.noticeNullText.visibility = View.VISIBLE
                    }else{
                        mAdapter.setList(mViewModel.mNoticeList)
                    }
                }
                is Failed -> {
                    LoggerUtils.d(TAG,"加载失败"+it.message)
                }
            }
        }
    }

    private fun gotoLogin(){
        ARouter.getInstance().build(RouterPath.LOGIN_UI).navigation()
    }
}